
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Data;
using System.Data.Common;

using Microsoft.ApplicationBlocks.Data;

namespace KioskViewerDAL.SqlServer
{
    public sealed class Untils
    {
        public static string connectionString = string.Empty;
        public static string dataProvider = ConfigurationManager.AppSettings.Get("DataProvider");
        public static string ConnectionString{
            get{
                if (connectionString == string.Empty)
                {
                    try
                    {
                        connectionString = ConfigurationManager.ConnectionStrings[dataProvider].ConnectionString;
                    }
                    catch (Exception)
                    {
                    }
                }
                else
                {
                    //KioskViewer.SystemEvent.getConnection();
                }
                return connectionString;
            }
            set{
                   connectionString=value;
            }
            //ConfigurationManager.ConnectionStrings[dataProvider].ConnectionString;
        }
        #region Global Excute Database       
        /// <summary>
        /// 
        /// Excute None Return Query
        /// </summary>
        /// <param name="Script">script value</param>
        /// <returns> error message if it's occur otherwise empty</returns>
        public static string ExecuteNonQuery(string Script)
        {
            try
            {
                SqlHelper.ExecuteNonQuery(ConnectionString, CommandType.Text, Script);
            }
            catch (Exception ex)
            {
                return ex.Message;
            };
            return string.Empty;
        }
        /// <summary>
        /// Excute None Return store procedure
        ///
        /// </summary>
        /// <param name="spName">procedure name</param>
        /// <param name="parameterValues">store procedure parameters</param>
        /// <returns>error message if it's occur otherwise empty</returns>
        public static string ExecuteNonQuery(string spName, params object[] parameterValues)
        {           
            try
            {
                SqlHelper.ExecuteNonQuery(ConnectionString, spName, parameterValues);
            }
            catch (Exception ex)
            {
                return ex.Message;
            };
            return string.Empty;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="SQL"></param>
        /// <param name="sErr"></param>
        /// <returns></returns>
        public static IDataReader ExecuteReaderEx(string SQL, ref string sErr)
        {
            sErr = string.Empty;
            try
            {
                return ((IDataReader)(SqlHelper.ExecuteReader(ConnectionString, CommandType.Text, SQL)));
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            return null;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="spName"></param>
        /// <param name="sErr"></param>
        /// <param name="parameterValues"></param>
        /// <returns></returns>
        public static IDataReader ExecuteReaderEx(string spName, ref string sErr, params object[] parameterValues)
        {
            sErr = string.Empty;
            try
            {
                return ((IDataReader)(SqlHelper.ExecuteReader(ConnectionString, spName, parameterValues)));
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            return null;
        }

        /// <summary>
        /// Run a return values query
        /// </summary>
        /// <param name="SQL">TSQL </param>
        /// <param name="sErr">error message if it's occur otherwise empty</param>
        /// <returns>Datata set contain return value </returns>
        public static DataSet ExecuteReader(string SQL, ref string sErr)
        {
            sErr = string.Empty;
            try
            {
                return ConvertDataReaderToDataSet(((IDataReader)(SqlHelper.ExecuteReader(ConnectionString, CommandType.Text, SQL))));
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            return null;
        }
       

        /// <summary>
        /// Excute a store procedure 
        /// </summary>
        /// <param name="spName">store procedure  name</param>
        /// <param name="sErr">error message if it's occur otherwise empty</param>
        /// <param name="parameterValues">store procedure parameters</param>
        /// <returns></returns>
        public static DataSet ExecuteReader(string spName, ref string sErr, params object[] parameterValues)
        {
            sErr = string.Empty;
            try
            {
                return ConvertDataReaderToDataSet(((IDataReader)(SqlHelper.ExecuteReader(ConnectionString, spName, parameterValues))));
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            return null;
        }
        /// <summary>
        ///Excute a Scalar TSQL 
        /// </summary>
        /// <param name="SQL">TSQL </param>
        /// <param name="sErr">error message if it's occur otherwise empty </param>
        /// <returns>Return vale as object type</returns>
        public static object ExecuteScalar(string SQL, ref string sErr)
        {
            sErr = string.Empty;
            try
            {
                return SqlHelper.ExecuteScalar(ConnectionString, CommandType.Text, SQL);
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            return null;
        }
        /// <summary>
        /// Excute a Scalar store procedure 
        /// </summary>
        /// <param name="spName">store procedure name</param>
        /// <param name="sErr">error message if it's occur otherwise empty</param>
        /// <param name="parameterValues">prammeters list/param>
        /// <returns></returns>
        public static object ExecuteScalar(string spName, ref string sErr, params object[] parameterValues)
        {
            sErr = string.Empty;
           
            try
            {
                return SqlHelper.ExecuteScalar(ConnectionString, spName, parameterValues);
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            return null;
        }

        //--------------------------------///
        /// <summary>
        /// Convert IDataReader to dataset
        /// </summary>
        /// <param name="reader"></param>
        /// <returns></returns>
        public static DataSet ConvertDataReaderToDataSet(IDataReader reader)
        {
            DataSet objDataSet = new DataSet();
            do
            {
                objDataSet.Tables.Add(ConvertDataReaderToDataTable(reader, false));
            } while (reader.NextResult());
            reader.Close();
            return objDataSet;
        }
        /// <summary>
        /// Convert IDataReader to Data table
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="bClose"></param>
        /// <returns></returns>
        public static DataTable ConvertDataReaderToDataTable(IDataReader reader, bool bClose)
        {
            DataTable objDataTable = new DataTable();
            int intFieldCount = reader.FieldCount;
            int intCounter;
            for (intCounter = 0; intCounter <= intFieldCount - 1; intCounter++)
            {
                objDataTable.Columns.Add(reader.GetName(intCounter), reader.GetFieldType(intCounter));
            }
            objDataTable.BeginLoadData();
            object[] objValues = new object[intFieldCount];
            while (reader.Read())
            {
                reader.GetValues(objValues);
                objDataTable.LoadDataRow(objValues, true);
            }
            if (bClose)
            {
                reader.Close();
            }
            objDataTable.EndLoadData();
            return objDataTable;
        }
        /// <summary>
        /// get company name from app assembly
        /// </summary>
        /// <returns></returns>
        public static String GetCompanyName()
        {
            String companyName = "Studio One Media, Inc";//default company name
            try
            {
                object[] assemblyObjects = System.Reflection.Assembly.GetEntryAssembly().GetCustomAttributes(typeof(System.Reflection.AssemblyCompanyAttribute), true);

                if (assemblyObjects.Length > 0)
                {

                    //assemblyGuid = new Guid(((System.Runtime.InteropServices.GuidAttribute)assemblyObjects[0]).Value);
                    companyName = ((System.Reflection.AssemblyCompanyAttribute)assemblyObjects[0]).Company;
                }
            }
            catch (Exception)
            {
            }
            return companyName;
        }
        /// <summary>
        /// get product name from app assembly
        /// </summary>
        /// <returns></returns>
        public static String GetProductName()
        {
             String appName = "StudioWorker";//default company name
             try
             {
                 object[] assemblyObjects = System.Reflection.Assembly.GetEntryAssembly().GetCustomAttributes(typeof(System.Reflection.AssemblyProductAttribute), true);

                 if (assemblyObjects.Length > 0)
                 {


                     appName = ((System.Reflection.AssemblyProductAttribute)assemblyObjects[0]).Product;
                 }
             }
             catch (Exception)
             {
             }
            return appName;
        }
        ///// <summary>
        ///// get Program Files of Windows system
        ///// </summary>
        ///// <returns></returns>
        //public static String GetProgramFilesPath()
        //{


        //    //String path = System.Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) + "\\" + GetCompanyName() + "\\" + GetProductName() + "\\";
        //    //String path = System.Windows.Forms.Application.StartupPath.TrimEnd('\\') + "\\";
        //    CreateFolder(path);
        //    return path;
            
        //}
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Path"></param>
        public static void CreateFolder(String path)
        {
            if (!System.IO.Directory.Exists(path))
            {

                if (path.Substring(0, 1).Equals("\\"))
                {
                    //System.Security.AccessControl.DirectorySecurity ds = new System.Security.AccessControl.DirectorySecurity();                
                    String[] p = path.Split('\\');

                    System.Security.AccessControl.DirectorySecurity ds = System.IO.Directory.GetAccessControl("\\\\" + p[2] + "\\" + p[3]);
                    System.IO.Directory.CreateDirectory(path, ds);
                }
                else
                {
                    String[] p = path.Split('\\');
                    System.Security.AccessControl.DirectorySecurity ds;
                    if (p.Length >= 2)
                    {
                        ds = System.IO.Directory.GetAccessControl(p[0] + "\\" + p[1]);
                    }
                    else
                    {
                        ds = System.IO.Directory.GetAccessControl(p[0] + "\\");
                    }
                    System.IO.Directory.CreateDirectory(path, ds);
                }




            }
        }
        #endregion

    }
}
